package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;


/**
 * Created by sunzhigang on 2015/8/14.
 * 要显示选择导航地图的对话框
 */
public class SelectMapNaviDialog extends Dialog{
    private MapSelectedCallBack mapselectedCallBack;
    private LinearLayout mBaiduLayout;
    private LinearLayout mAmapLayout;
    private String mTitle;
    private TextView mTv_Title;
    private String watchOwnerName;
    private  TextView watch_tv;

    public SelectMapNaviDialog(Context context, MapSelectedCallBack mapselectedCallBack
            , String mTitle, String watchOwnerName) {
        super(context);
        this.mapselectedCallBack = mapselectedCallBack;
        this.mTitle = mTitle;
        this.watchOwnerName = watchOwnerName;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_navi_dialog);


        setDialogAttributes();
        initDialogView();
    }

    private void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private void initDialogView() {
        mBaiduLayout = (LinearLayout) findViewById(R.id.blinearLayout);
        mAmapLayout = (LinearLayout) findViewById(R.id.glinearLayout);
        mTv_Title = (TextView) findViewById(R.id.show_title);
        watch_tv = (TextView) findViewById(R.id.watcher_owner);

        watch_tv.setText(watchOwnerName);
        mTv_Title.setText(mTitle);
        mBaiduLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mapselectedCallBack.onMapSelected("com.baidu.BaiduMap");
                dismiss();
            }
        });
        mBaiduLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    mAmapLayout.setEnabled(false);
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    mAmapLayout.setEnabled(true);
                }
                return false;
            }
        });
        mAmapLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBaiduLayout.setEnabled(true);
                mapselectedCallBack.onMapSelected("com.autonavi.minimap");
                dismiss();
            }
        });
        mAmapLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN)
                {
                    mBaiduLayout.setEnabled(false);
                }else if (motionEvent.getAction() == MotionEvent.ACTION_UP)
                {
                    mBaiduLayout.setEnabled(true);
                }
                return false;
            }
        });
    }

    public interface MapSelectedCallBack {
        void onMapSelected(String packageName);
    }
}
